import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({ showSpinner: false }) // 关闭螺旋

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/Login/index.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/login/callback',
      name: 'loginCallback',
      component: () => import('@/views/Login/loginCallback.vue'),
      meta: {
        title: 'QQ登录'
      }
    },
    {
      path: '/',
      redirect: '/home',
      component: () => import('@/views/Layout/index.vue'),
      children: [
        {
          path: '/home',
          name: 'home',
          component: () => import('@/views/Home/index.vue'),
          meta: {
            title: '首页'
          }
        },
        {
          path: '/fitness',
          name: 'fitness',
          component: () => import('@/views/Fitness/index.vue'),
          meta: {
            title: '健康百科'
          }
        },
        {
          path: '/notice',
          name: 'notice',
          component: () => import('@/views/Notice/index.vue'),
          meta: {
            title: '消息中心'
          }
        },
        {
          path: '/user',
          name: 'user',
          component: () => import('@/views/User/index.vue'),
          meta: {
            title: '我的'
          }
        }
      ]
    },
    {
      path: '/user/patient',
      component: () => import('@/views/User/UserPatient.vue'),
      name: 'patient',
      meta: {
        title: '家庭档案'
      }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult/FastConsult.vue'),
      name: 'faseConsult',
      meta: {
        title: '极速问诊'
      }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/Consult/DepConsult.vue'),
      name: 'DepConsult',
      meta: {
        title: '选择科室'
      }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult/IllnessConsult.vue'),
      name: 'IllnessConsult',
      meta: {
        title: '图文问诊'
      }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/Consult/PayConsult.vue'),
      name: 'PayConsult',
      meta: {
        title: '问诊支付'
      }
    },
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      name: 'ConsultRoom',
      meta: {
        title: '问诊室'
      }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/User/ConsultRecord.vue'),
      name: 'ConsultRecord',
      meta: {
        title: '问诊记录'
      }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/Consult/ConsultDetail.vue'),
      name: 'ConsultDetail',
      meta: {
        title: '问诊详情'
      }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/Pay/index.vue'),
      name: 'OrderPay',
      meta: {
        title: '药品支付'
      }
    },
    {
      path: '/order/address',
      component: () => import('@/views/Pay/UserAddress.vue'),
      name: 'UserAddress',
      meta: {
        title: '收货地址'
      }
    },
    {
      path: '/order/pay/result',
      component: () => import('@/views/Pay/PayResult.vue'),
      name: 'PayResult',
      meta: {
        title: '支付结果'
      }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/Pay/OrderDetail.vue'),
      name: 'OrderDetail',
      meta: {
        title: '药品订单详情'
      }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/Pay/LogisticsDetail.vue'),
      name: 'LogisticsDetail',
      meta: {
        title: '物流详情'
      }
    }
  ]
})
// 设置路由白名单，即不需要token就可以登录的url地址
const whiteRoute = ['/login', '/login/callback']
// 前置导航守卫，进行权限控制
router.beforeEach((to) => {
  NProgress.start() // 开启进度条
  // 跳转前修改网页标题
  document.title = `优医问诊-${to.meta.title}`
  const user = useUserStore()
  // 判断用户是否有token
  // 如果没有token,然后判断用户去往的页面是否在白名单内，如果在则放行，否则跳转到登录页面 => return '/login | return {name: 'login}
  if (!user.user?.token && !whiteRoute.includes(to.path)) {
    return { name: 'login' }
  }
  // 如果有token，跳转到登录页面则让其跳回到原来的页面或者首页
  else if (user.user?.token && to.path === '/login') {
    return { name: 'user' }
  }
})
// 后置导航守卫，关闭进度条
router.afterEach(() => {
  NProgress.done()
})

export default router
